import { getCdList, siteLockStatus } from '../../api/index'
import { getYMDHMS } from '../../utils/util'
Page({

  /* 页面的初始数据 */
  data: {
    projectId: '',
    nowe: getYMDHMS(new Date()).slice(0, 10),
    siteModelList: [],
    siteModelIds: [],
    name: wx.getStorageSync('venueName'),   //场馆名称
    des: '',
    zq: '',
    siteModelName: '',
    kssj: '',
    jssj: '',
    startTime: '',
    endTime: '',
    checkList: [{
      value: '7',
      label: '周日'
    },
    {
      value: '1',
      label: '周一'
    },
    {
      value: '2',
      label: '周二'
    },
    {
      value: '3',
      label: '周三'
    },
    {
      value: '4',
      label: '周四'
    },
    {
      value: '5',
      label: '周五'
    },
    {
      value: '6',
      label: '周六'
    }],
    result: [],
    showCheck: false,
    showSite: false,
    siteId: [],
    dcxz: ''
  },
  bindTimeChange: function (e) {
    let str = e.detail.value
    let ch = "-";
    let val = str.replace(new RegExp(ch, 'g'), "/");
    if (this.data.jssj) {
      let valb = this.data.jssj.replace(new RegExp(ch, 'g'), "/");
      if (new Date(val).getTime() >= new Date(valb).getTime()) {
        return wx.showToast({ title: '开始日期需要小于结束日期', icon: 'none', duration: 2000 })
      }
    }
    let valc = this.data.nowe.replace(new RegExp(ch, 'g'), "/");
    if (new Date(val).getTime() < new Date(valc).getTime()) {
      return wx.showToast({ title: '预定只能选择今天及之后', icon: 'none', duration: 2000 })
    }
    this.setData({
      kssj: e.detail.value
    })
  },
  bindTimeChangeb: function (e) {
    let str = e.detail.value
    if (this.data.kssj) {
      let ch = "-";
      let val = str.replace(new RegExp(ch, 'g'), "/");
      let valb = this.data.kssj.replace(new RegExp(ch, 'g'), "/");
      if (new Date(val).getTime() <= new Date(valb).getTime()) {
        return wx.showToast({ title: '开始日期需要小于结束日期', icon: 'none', duration: 2000 })
      }
    }
    this.setData({
      jssj: e.detail.value
    })
  },
  bindTimeChangec: function (e) {
    let str = e.detail.value
    let ch = "-";
    let val = str.replace(new RegExp(ch, 'g'), "/");
    if (this.data.endTime) {
      let valb = this.data.endTime.replace(new RegExp(ch, 'g'), "/");
      if (new Date('2021/01/01 ' + val + ':00').getTime() >= new Date('2021/01/01 ' + valb + ':00').getTime()) {
        return wx.showToast({ title: '开始时间需要小于结束时间', icon: 'none', duration: 2000 })
      }
    }
    let valc = this.data.nowe.replace(new RegExp(ch, 'g'), "/");
    if (new Date(val).getTime() < new Date(valc).getTime()) {
      return wx.showToast({ title: '开始时间需要小于结束时间', icon: 'none', duration: 2000 })
    }
    this.setData({
      startTime: e.detail.value
    })
  },
  bindTimeChanged: function (e) {
    let str = e.detail.value
    if (this.data.startTime) {
      let ch = "-";
      let val = str.replace(new RegExp(ch, 'g'), "/");
      let valb = this.data.startTime.replace(new RegExp(ch, 'g'), "/");
      if (new Date('2021/01/01 ' + val + ':00').getTime() <= new Date('2021/01/01 ' + valb + ':00').getTime()) {
        return wx.showToast({ title: '开始时间需要小于结束时间', icon: 'none', duration: 2000})
      }
    }
    this.setData({
      endTime: e.detail.value
    })
  },

  // 关闭密码输入弹框
  onClose() {
    this.setData({ show: false, pwd: '' });
  },
  onCloses() {
    this.setData({ showCheck: false });
  },
  onCloseb() {
    this.setData({ showSite: false });
  },
  // 实时获取密码输入框值
  bindKeyInput(e) {
    this.setData({
      pwd: e.detail.value
    })
  },


  // 提交订单
  submit() {
    if (this.data.siteModelIds.length == 0) return wx.showToast({ title: '请选择锁定场地', icon: 'none', duration: 2000 })
    if (!this.data.kssj) return wx.showToast({ title: '请选择开始日期', icon: 'none', duration: 2000 })
    if (!this.data.jssj) return wx.showToast({ title: '请选择结束日期', icon: 'none', duration: 2000 })
    if (!this.data.startTime) return wx.showToast({ title: '请选择开始时间', icon: 'none', duration: 2000 })
    if (!this.data.endTime) return wx.showToast({ title: '请选择结束时间', icon: 'none', duration: 2000 })
    if (this.data.result.length == 0) return wx.showToast({ title: '请选择锁定周期', icon: 'none', duration: 2000 })
    siteLockStatus({
      param: {
        endDate: this.data.jssj,
        projectId: this.data.projectId,
        endTime: this.data.endTime,
        startTime: this.data.startTime,
        siteModelIds: this.data.siteModelIds,
        remark: this.data.des,
        startDate: this.data.kssj,
        type: '0',
        days: this.data.result.join(',')
      }
    }).then(res => {
      if (res.errorCode == '000000') {
        wx.showToast({
          title: '锁场成功',
          icon: 'success',
          duration: 2000,
          mask: true,
          success(res) {
            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000)
          }
        })
      }
    })
  },
  hyzqClick() {
    this.setData({ showCheck: true })
  },
  showCd() {
    this.setData({ showSite: true })
  },
  zqBtn() {
    let result = this.data.result
    let checkList = this.data.checkList
    let arr = []
    for(var i = 0; i < checkList.length; i++){
      for(var s = 0; s < result.length; s++){
        if(checkList[i].value == result[s] ){
          arr.push(checkList[i].label)
        }
      }
    }
    this.setData({ showCheck: false, zq: arr.join(',') })
  },
  zqBtnb() {
    let result = this.data.siteModelIds
    let checkList = this.data.siteModelList
    let arr = []
    for(var i = 0; i < checkList.length; i++){
      for(var s = 0; s < result.length; s++){
        if(checkList[i].id == result[s] ){
          arr.push(checkList[i].siteName)
        }
      }
    }
    this.setData({ showSite: false, siteModelName: arr.join(',') })
  },
  onChanges(event) {
    this.setData({
      result: event.detail,
    });
  },
  onChangeb(event) {
    this.setData({
      siteModelIds: event.detail,
    });
  },

  toggle(event) {
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  getCdList() {
    getCdList({ param: { projectId: this.data.projectId } }).then(res => {
      if (res.errorCode != '000000') return
      this.setData({
        siteModelList: res.recordList || []
      })
    }).catch(err => {
      console.log(err)
    })
  },

  /* 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    this.setData({ projectId: options.id })
    this.getCdList()
  },

  /* 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({ name: wx.getStorageSync('venueName') })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})